package com.soilflow.info.service.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.soilflow.common.core.domain.entity.SysUserReception;
import com.soilflow.common.domain.SysArticle;
import com.soilflow.common.domain.SysCollect;
import com.soilflow.common.domain.SysLike;
import com.soilflow.common.domain.dto.SysLikeDto;
import com.soilflow.common.domain.vo.SysCollectVo;
import com.soilflow.common.domain.vo.SysLikeVo;
import com.soilflow.common.excle.DuocaiCommentExcel;
import com.soilflow.common.excle.DuocaiLLikeExcell;
import com.soilflow.common.utils.DateUtils;
import com.soilflow.common.utils.SecurityUtils;
import com.soilflow.common.utils.StringUtils;
import com.soilflow.info.mapper.SysCollectMapper;
import com.soilflow.info.mapper.SysLikeMapper;
import com.soilflow.info.service.ISysArticleService;
import com.soilflow.info.service.ISysCollectService;
import com.soilflow.info.service.ISysLikeService;
import com.soilflow.system.service.IColorfulUserService;
import com.soilflow.system.service.ISysUserReceptionService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author party
 * @date 2023-10-09
 */
@Service
public class SysLikeServiceImpl extends ServiceImpl<SysLikeMapper, SysLike> implements ISysLikeService {
    @Resource
    private SysLikeMapper sysLikeMapper;
    @Autowired
    private ISysUserReceptionService userReceptionService;

    /**
     * 查询【请填写功能名称】
     * 
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public SysLike selectSysLikeById(Long id)
    {
        return sysLikeMapper.selectSysLikeById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param sysLike 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<SysLike> selectSysLikeList(SysLike sysLike) {
        return sysLikeMapper.selectSysLikeList(sysLike);
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param  【请填写功能名称】
     * @return 结果
     */
    @Override
    public boolean insertSysLike(SysLikeDto dto) {
        boolean staus = dto.getStaus();
        Long userId = dto.getUserId();
        SysUserReception userReception = userReceptionService.selectUserById(userId);
        if (staus) {
            //为true则是表示用户收藏了,这里进行修改状态，变为不显示收藏
            SysLike sysLike = new SysLike();
            BeanUtils.copyProperties(dto,sysLike);
            return this.updateById(sysLike);
        } else {
            //false则表示用户没有收藏,点击这里做判断
            //查询数据库，如果没有这条数据在新增一条，有但状态为1则进行修改
            LambdaQueryWrapper<SysLike> wrapper=new LambdaQueryWrapper<>();
            wrapper.eq(SysLike::getCreateId,dto.getUserId());
            wrapper.eq(SysLike::getLandId,dto.getLandId());
            SysLike like = this.getOne(wrapper);
            if (like == null) {
                SysLike sysLike = new SysLike();
                BeanUtils.copyProperties(dto, sysLike);
                sysLike.setCreateId(userId);
                sysLike.setCreateTime(DateUtils.getNowDate());
                sysLike.setCreateBy(userReception.getUserName());
                sysLike.setLikeUserNikename(userReception.getNickName());
                return this.save(sysLike);
            } else {
                SysLike sysLike = new SysLike();
                BeanUtils.copyProperties(dto,sysLike);
                return this.updateById(sysLike);
            }

        }
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param sysLike 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateSysLike(SysLike sysLike)
    {
        sysLike.setUpdateTime(DateUtils.getNowDate());
        return sysLikeMapper.updateSysLike(sysLike);
    }

    /**
     * 批量删除【请填写功能名称】
     * 
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteSysLikeByIds(Long[] ids)
    {
        return sysLikeMapper.deleteSysLikeByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteSysLikeById(Long id)
    {
        return sysLikeMapper.deleteSysLikeById(id);
    }

    @Override
    public List<SysArticle> sortLike(String sort) {
//        List<SysArticle> articles=new ArrayList<>();
//        LambdaQueryWrapper<SysLike> wrapper=new LambdaQueryWrapper<>();
//        SysArticle sysArticle = new SysArticle();
//        if (StringUtils.isEmpty(sort)) {
//            return sysArticleService.selectarticle(sysArticle);
//        }
//        if (sort.equals("1")) {
////            wrapper.orderByDesc(SysLike::getViewCount);
//            List<SysLike> likes = this.list(wrapper);
//            for (SysLike like : likes) {
//                SysArticle article = sysArticleService.getById(like.getArticleId());
//                Long id = article.getId();
//                //
////                article.setLikeCount(like.getViewCount());
//                //获取收藏量
//                LambdaQueryWrapper<SysCollect> queryWrapper=new LambdaQueryWrapper<>();
//                queryWrapper.eq(SysCollect::getArticleId,id);
//                SysCollect collect = sysCollectService.getOne(queryWrapper);
////                article.setViewCount(collect.getViewCount());
//                articles.add(article);
//            }
//            return articles;
//        } else if (sort.equals("0")) {
////            wrapper.orderByAsc(SysLike::getViewCount);
//            List<SysLike> likes = this.list(wrapper);
//            for (SysLike like : likes) {
//                SysArticle article = sysArticleService.getById(like.getArticleId());
//                Long id = article.getId();
//                //
////                article.setLikeCount(like.getViewCount());
//                //获取收藏量
//                LambdaQueryWrapper<SysCollect> queryWrapper=new LambdaQueryWrapper<>();
//                queryWrapper.eq(SysCollect::getArticleId,id);
//                SysCollect collect = sysCollectService.getOne(queryWrapper);
////                article.setViewCount(collect.getViewCount());
//                articles.add(article);
//            }
//            return articles;
//        }
//        return sysArticleService.selectarticle(sysArticle);
        return null;
    }

    @Override
    public void export(HttpServletResponse response) throws IOException {
         List<DuocaiLLikeExcell> likeExcells = this.baseMapper.export();
        EasyExcel.write(response.getOutputStream(), DuocaiLLikeExcell.class).sheet().doWrite(likeExcells);
    }

    @Override
    public SysLikeVo userCollect(String id, Long userId) {
        Long ids=Long.parseLong(id);
        LambdaQueryWrapper<SysLike> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(SysLike::getCreateId,userId);
        wrapper.eq(SysLike::getLandId,ids);
        SysLike like = this.getOne(wrapper);
        SysLikeVo sysLikeVo = new SysLikeVo();
        if (like != null) {
            if (like.getStatus() == 0) {
                sysLikeVo.setStatus(true);
                sysLikeVo.setSysLikeId(like.getId());
            } else if (like.getStatus() == 1) {
                sysLikeVo.setStatus(false);
                sysLikeVo.setSysLikeId(like.getId());
            }
            return sysLikeVo;
        } else {
            sysLikeVo.setStatus(false);
            sysLikeVo.setSysLikeId(null);
            return sysLikeVo;
        }
    }
}
